<?php
namespace app\system\model;
/**
 * 内容工具
 */
class ToolsModel {

    /**
     * 获取内容指定图片
     * @param string $content 内容
     * @param int $num 第N张图片
     * @return string 图片URL
     */
    public function getImage($content, $num = 1)
    {
        $content = html_out($content);
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"]/i', $content, $matches);
        $num = $num - 1;
        $img = $matches[1][$num];
        return $img;
    }

    /**
     * 获取关键词
     * @param string $title 标题
     * @param string $content 内容
     * @return string 图片URL
     */
    public function getKerword($title, $content = '')
    {
        $data= \variant\ext\Http::doGet('http://keyword.discuz.com/related_kw.html?ics=utf-8&ocs=utf-8&title='.urlencode($title).'&content='.urlencode($content),10);
        if(empty($data)){
            return;
        }
        preg_match_all("/<kw>(.*)A\[(.*)\]\](.*)><\/kw>/",$data, $list, PREG_SET_ORDER);
        if(empty($list)){
            return;
        }
        $keywords = array();
        foreach ($list as $value) {
            $keywords[] = $value[2];
        }
        return implode(',', $keywords);
        
    }

}
